package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.SysCommunity;
import com.ruoyi.community.service.ISysCommunityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 社区Controller
 * 
 * @author 韩宁
 * @date 2021-08-01
 */
@RestController
@RequestMapping("/system/community")
public class SysCommunityController extends BaseController
{
    @Autowired
    private ISysCommunityService sysCommunityService;


    /**
     * 查询用户所属社区列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysCommunity sysCommunity)
    {
        startPage();
        List<SysCommunity> list = sysCommunityService.selectSysCommunityList(sysCommunity);
        return getDataTable(list);
    }

    /**
     * 查询用户未加入的社区列表
     */
    @GetMapping("/list/other")
    public TableDataInfo listOther()
    {
        List<SysCommunity> list = sysCommunityService.selectListOther(SecurityUtils.getUserID());
        return getDataTable(list);
    }

    /**
     * 查询全部小区
     * @return
     */
    @GetMapping("/list/all")
    public TableDataInfo comList(){
        List<SysCommunity> list = sysCommunityService.getAllCommunity();
        return getDataTable(list);
    }

    /**
     * 导出社区列表
     */
    @PreAuthorize("@ss.hasPermi('system:community:export')")
    @Log(title = "社区", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCommunity sysCommunity)
    {
        List<SysCommunity> list = sysCommunityService.selectSysCommunityList(sysCommunity);
        ExcelUtil<SysCommunity> util = new ExcelUtil<SysCommunity>(SysCommunity.class);
        return util.exportExcel(list, "社区数据");
    }

    /**
     * 获取社区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:community:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        SysCommunity community = sysCommunityService.selectSysCommunityById(id);

        return AjaxResult.success(community);
    }

    /**
     * 新增社区
     */
//    @PreAuthorize("@ss.hasPermi('system:community:add')")
    @Log(title = "社区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCommunity sysCommunity)
    {
        return toAjax(sysCommunityService.insertSysCommunity(sysCommunity));
    }

    /**
     * 修改社区
     */
    @PreAuthorize("@ss.hasPermi('system:community:edit')")
    @Log(title = "社区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCommunity sysCommunity)
    {
        Long userID = SecurityUtils.getUserID();
        return userID.equals(sysCommunity.getUserId()) ?
                toAjax(sysCommunityService.updateSysCommunity(sysCommunity)) :
                AjaxResult.error("您没有修改"+ sysCommunity.getName() +"的权限");
    }

    /**
     * 删除社区
     */
    @PreAuthorize("@ss.hasPermi('system:community:remove')")
    @Log(title = "社区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysCommunityService.deleteSysCommunityByIds(ids));
    }


}
